package com.lalifa.extension

import android.graphics.Rect
import android.media.MediaMetadataRetriever
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.appcompat.widget.AppCompatImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.MultiTransformation
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.load.resource.bitmap.FitCenter
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.lalifa.base.R
import com.lalifa.ext.Tools
import com.lalifa.groupavatars.GroupAvatarsLib
import com.lalifa.groupavatars.layout.DingLayoutManager
import com.lalifa.groupavatars.layout.ILayoutManager
import com.lalifa.transformation.MosaicBitmapTransformation
import com.lalifa.utils.BlurTransformation
import com.lalifa.widget.GridImageView
import com.lalifa.widget.XBaseAdapter


/**
 *
 * @ClassName ImageViewExtension
 * @Author lwj
 * @Email 1036046880@qq.com
 * @Date 2021/12/24 17:10
 *
 */

/**
 * 加载图片
 * @receiver ImageView
 * @param url String
 * @param resourceId Int
 */
fun ImageView.loadLocal(resourceId: Int = R.drawable.mx_common_divider) {
    Glide.with(this.context).load(resourceId).into(this)
}

/**
 * 加载图片
 * @receiver ImageView
 * @param url String
 * @param resourceId Int
 */
fun AppCompatImageView.loadLocal(resourceId: Int = R.drawable.mx_common_divider) {
    Glide.with(this.context).load(resourceId).into(this)
}

/**
 * 加载图片
 * @receiver ImageView
 * @param url String
 * @param resourceId Int
 */
fun AppCompatImageView.load(url: String?, resourceId: Int = R.drawable.mx_common_divider) {
    Glide.with(this.context).load(getImageUrl(url.pk())).error(resourceId).into(this)
}

/**
 * 加载图片
 * @receiver ImageView
 * @param url String
 * @param resourceId Int
 */
fun AppCompatImageView.loadFile(url: String, resourceId: Int = R.drawable.mx_common_divider) {
    Glide.with(this.context).load(url).error(resourceId).into(this)
}

/**
 * 加载图片
 * @receiver ImageView
 * @param url String
 * @param resourceId Int
 */
fun ImageView.loadFile(url: String, resourceId: Int = R.drawable.mx_common_divider) {
    Glide.with(this.context).load(url).error(resourceId).into(this)
}

/**
 * 加载图片
 * @receiver ImageView
 * @param url String
 * @param resourceId Int
 */
fun ImageView.load(url: String?, resourceId: Int = R.drawable.mx_common_divider) {
    Glide.with(this.context).load(getImageUrl(url.pk())).error(resourceId).into(this)
}

fun getImageUrl(url: String) : String {
    return if(url?.contains("http") == true) url else Tools.HOST + url
}

/**
 * 加载圆形图片
 * @receiver ImageView
 * @param url String
 * @param resourceId Int
 */
fun ImageView.loadCircle(url: String, resourceId: Int = R.drawable.mx_common_divider) {
    Glide.with(this.context).load(getImageUrl(url)).error(resourceId).circleCrop()
        .into(this)
}

/**+
 * 加载圆角图
 * @receiver ImageView
 * @param url String
 * @param radius Int
 * @param resourceId Int
 */
fun ImageView.loadRound(
    url: String,
    radius: Int = 10,
    centerCrop: Boolean = false,
    resourceId: Int = R.drawable.mx_common_divider
) {
    Glide.with(this.context).load(getImageUrl(url.pk())).apply(
        RequestOptions().error(resourceId).transform(
            MultiTransformation(
                if (centerCrop) CenterCrop() else FitCenter(),
                RoundedCorners(radius.dp)
            )
        )
    ).into(this)
}

/**
 * 加载马赛克
 * @receiver ImageView
 * @param url String
 * @param rect Rect?
 * @param size Int
 * @param circleCrop Boolean
 * @param resourceId Int
 */
fun ImageView.loadMosaic(
    url: String,
    rect: Rect? = null,
    size: Int = 20,
    circleCrop: Boolean = false,
    resourceId: Int = R.drawable.mx_common_divider
) {
    Glide.with(this.context).load(url).apply(
        RequestOptions().error(resourceId).transform(
            MultiTransformation(
                MosaicBitmapTransformation(rect, size),
                if (circleCrop) CircleCrop() else CenterCrop()
            )
        )
    ).into(this)
}

/**
 * 高斯模糊
 * @receiver ImageView
 * @param url String
 * @param blurRadius Int
 * @param centerCrop Boolean
 * @param resourceId Int
 */
fun ImageView.loadBlur(
    url: String,
    blurRadius: Int = 10,
    centerCrop: Boolean = false,
    resourceId: Int = R.drawable.mx_common_divider
) {
    Glide.with(this.context).load(url).apply(
        RequestOptions().error(resourceId).transform(
            MultiTransformation(
                if (centerCrop) CenterCrop() else FitCenter(),
                BlurTransformation(blurRadius.dp)
            )
        )
    ).into(this)
}

/**
 * 高斯模糊
 * @receiver ImageView
 * @param url String
 * @param blurRadius Int
 * @param centerCrop Boolean
 * @param resourceId Int
 */
fun ImageView.loadBlur(
    res: Int,
    blurRadius: Int = 10,
    blurSampling: Int = 1,
    centerCrop: Boolean = true,
    resourceId: Int = R.drawable.mx_common_divider
) {
    Glide.with(this.context).load(res).apply(
        RequestOptions().error(resourceId).transform(
            MultiTransformation(
                if (centerCrop) CenterCrop() else FitCenter(),
                BlurTransformation(blurRadius,blurSampling)
            )
        )
    ).into(this)
}

/**
 * 加载网络视频封面
 * @receiver ImageView
 * @param url String
 */
fun ImageView.loadVideoImg(url: String) {
    val mmr = MediaMetadataRetriever()
    mmr.setDataSource(url, hashMapOf())
    setImageBitmap(mmr.getFrameAtTime(-1))
    mmr.release()
}